AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
AddCSLuaFile("sh_class.lua")
AddCSLuaFile("cl_panels.lua")
AddCSLuaFile("sh_game.lua")
AddCSLuaFile("sh_team.lua")
AddCSLuaFile("cl_sound.lua")
AddCSLuaFile("cl_toppanels.lua")

include( 'shared.lua' )
include("game/init.lua")
include("player.lua")
include("flag.lua")
include("block/init.lua")
include("download.lua")
include("log.lua")

resource.AddFile("sound/ctf/reddrop.wav")
resource.AddFile("sound/ctf/redhas.wav")
resource.AddFile("sound/ctf/redreturn.wav")
resource.AddFile("sound/ctf/redscore.wav")
resource.AddFile("sound/ctf/redwin.wav")
resource.AddFile("sound/ctf/bluedrop.wav")
resource.AddFile("sound/ctf/bluehas.wav")
resource.AddFile("sound/ctf/bluereturn.wav")
resource.AddFile("sound/ctf/bluescore.wav")
resource.AddFile("sound/ctf/bluewin.wav")

local RemoveProhibitedProps = CreateConVar("nc_rocket_damange_scale", "0.33" )
local AllowGuildedMissles = CreateConVar("nc_rocket_guilded", "0" )

function GM:PlayerSelectSpawn( pl )
		
	local Entities = ents.FindByClass("info_player_team_spawn")
	local Filtered = {}
	local PlayerTeam = pl:Team()
	
	for _, ent in pairs( Entities ) do
		if ent:GetTeam() == PlayerTeam then
			table.insert( Filtered, ent )
		end	
	end
	
	if ( #Filtered == 0 ) then
		Msg("[PlayerSelectSpawn] Error! No spawn points!\n")
		return self.BaseClass.PlayerSelectSpawn( self, pl )
	end
	
	local ChosenSpawnPoint = nil
	
	// Try to work out the best, random spawnpoint (in 6 goes)
	for i=0, 6 do
	
		ChosenSpawnPoint = table.Random( Filtered )

		if ( ChosenSpawnPoint &&
			ChosenSpawnPoint:IsValid() &&
			ChosenSpawnPoint:IsInWorld() &&
			//ChosenSpawnPoint != pl:GetVar( "LastSpawnpoint" ) &&
			ChosenSpawnPoint != self.LastSpawnPoint ) then
			
			if ( GAMEMODE:IsSpawnpointSuitable( pl, ChosenSpawnPoint, i==6 ) ) then
			
				self.LastSpawnPoint = ChosenSpawnPoint
				//pl:SetVar( "LastSpawnpoint", ChosenSpawnPoint )
				return ChosenSpawnPoint
			
			end
			
		end
			
	end
	
	return ChosenSpawnPoint
	
end

hook.Add("PlayerNoClip", "DisableNoclip", function( ply )
	return ply:IsAdmin()
end)

hook.Add("InitPostEntity", "ChangeAmmoCreates", function()
	
	for _, crate in pairs( ents.FindByClass("item_ammo_crate") ) do
		
		local pos = crate:GetPos()
		local ang = crate:GetAngles()
		local skin = crate:GetSkin()
		
		crate:Remove()
		
		local ent = ents.Create("sf_ammo_crate")
		ent:SetPos( pos )
		ent:SetAngles( ang )
		ent:SetSkin( skin )
		
		ent:Spawn()
	
	
	end

end )

--[[
	1000 Appologies for this function but there seemes to be no other way
	I have tried using hooks into "OnEntityCreated" and "EntityKeyValue" but
	It seems like that none of them seems to always correctly predict when the rocket spawns
	I have also tried with "KeyPress", but players can just hold the mouse1 down and shoot again
]]
hook.Add("Think", "WTFRockets?", function()
	if AllowGuildedMissles:GetBool() then
		return
	end

	for _, ent in pairs( ents.FindByClass("rpg_missile") ) do
		if ent.OwnerRemoved == nil then
			ent.RocketOriginalOwner = ent:GetOwner()
			ent:SetOwner( nil )
			ent.OwnerRemoved = true
		end
	end
end )

hook.Add("EntityTakeDamage", "ChangeRocketOwner", function( ply, inflictor, attacker, amount, dmginfo )

	if ValidPlayer( ply ) && ValidEntity( inflictor ) && ValidEntity( inflictor.RocketOriginalOwner ) then
		dmginfo:ScaleDamage( RemoveProhibitedProps:GetFloat() )
		dmginfo:SetAttacker( inflictor.RocketOriginalOwner )
	end
	
end )	